$("#bidInput").keydown(function(e) {
    if (window.event.keyCode == 13) return false;
});
var minmoney = parseInt($("#minPrice").html().substr(1));
var maxmoney = parseInt($("#maxPrice").html().substr(1));
var chang = $("#slideBar").width();
var leftwidth = $("#slideInner").offset().left;
$(".public_tip .btn_default").click(function() {
    $("#publicTip").hide();
    $("#popupbg").hide();
});
$("#slideBar").mousedown(function(e) {
    move(e.pageX);
});
$("#bidInput").bind("focus keyup",
function(e) {
    this.value = this.value.replace(/\D/g, '');
    var money = parseInt(this.value);
    if (money >= minmoney && money <= maxmoney) {
        $("#bidError").attr("style", "display:none");
        inputmoney(leftwidth + (money - minmoney) / (maxmoney - minmoney) * chang);
        var bid = money;
        var chancePrice = $("#chancePrice_hidden").val();
        chancePrice = Number(chancePrice);
        var s1 = $("#seed1_hidden").val();
        var s2 = $("#seed2_hidden").val();
        s1 = Number(s1);
        s2 = Number(s2);
        if (chancePrice > 0) {
            chancePrice = (chancePrice - s2) / s1;
        }
        if (bid > chancePrice) {
            $("#bidTipDiv").hide();
        } else {
            var p = $("#p_hidden").val();
            p = Number(p);
            if (p > 0) {
                p = (p - s2) / s1;
            }
            if (bid >= p) {
                $("#bidTipDiv").html("<p>根据近期数据，您的成交概率较小</p>");
            } else {
                $("#bidTipDiv").html("<p>根据近期数据，您的成交概率为零</p>");
            }
            $("#bidTipDiv").show();
        }
    } else {
        $("#bidError").attr("style", "display:block");
    }
});
function inputmoney(xValue) {
    var proportion;
    var moneyinput = $("#bidInput");
    var x = xValue - leftwidth;
    proportion = (x / chang) * 100;
    if (x >= 0 && x <= chang) {
        $("#slideInner").attr("style", "width:" + proportion + "%;");
        $("#slideBtn").attr("style", "left:" + proportion + "%;");
    }
}
function move(xValue) {
    var proportion;
    var moneyinput = $("#bidInput");
    var x = xValue - leftwidth;
    proportion = (x / chang) * 100;
    if (x >= 0 && x <= chang) {
        $("#slideInner").attr("style", "width:" + proportion + "%;");
        $("#slideBtn").attr("style", "left:" + proportion + "%;");
        var bid = minmoney + parseInt((maxmoney - minmoney) * proportion / 100);
        moneyinput.attr("value", bid);
        var chancePrice = $("#chancePrice_hidden").val();
        chancePrice = Number(chancePrice);
        var s1 = $("#seed1_hidden").val();
        var s2 = $("#seed2_hidden").val();
        s1 = Number(s1);
        s2 = Number(s2);
        if (chancePrice > 0) {
            chancePrice = (chancePrice - s2) / s1;
        }
        if (bid > chancePrice) {
            $("#bidTipDiv").hide();
        } else {
            var p = $("#p_hidden").val();
            p = Number(p);
            if (p > 0) {
                p = (p - s2) / s1;
            }
            if (bid >= p) {
                $("#bidTipDiv").html("<p>根据近期数据，您的成交概率较小</p>");
            } else {
                $("#bidTipDiv").html("<p>根据近期数据，您的成交概率为零</p>");
            }
            $("#bidTipDiv").show();
        }
    }
}
var isClick = false;
$("#slideBar").mousedown(function() {
    isClick = true;
});
$("body").mousemove(function(e) {
    if (isClick) {
        move(e.pageX);
    }
});
$("body").mouseup(function() {
    isClick = false;
});
function bidChange() {
    var s1 = $("#seed1_hidden").val();
    var s2 = $("#seed2_hidden").val();
    s1 = Number(s1);
    s2 = Number(s2);
    var chancePrice = $("#chancePrice_hidden").val();
    chancePrice = Number(chancePrice);
  
    if (chancePrice > 0) {
        chancePrice = (chancePrice - s2) / s1;
    }
    var bid = $("#bidInput").val();
    bid = Number(bid);
    if (bid > chancePrice) {
        $("#bidTipDiv").hide();
    } else {
        var p = $("#p_hidden").val();
        p = Number(p);
        if (p > 0) {
            p = (p - s2) / s1;
        }
        if (bid >= p) {
            $("#bidTipDiv").html("<p>根据近期数据，您的成交概率较小</p>");
        } else {
            $("#bidTipDiv").html("<p>根据近期数据，您的成交概率为零</p>");
        }
        $("#bidTipDiv").show();
    }
}
function selectHotel(div) {
    var jDiv = $(div);
    var data = jDiv.attr("data");
    var arr = data.split("_");
    var hotelName = jDiv.attr("hotelName");
    var roomTypeName = jDiv.attr("roomTypeName");
    var elongPrice = jDiv.attr("elongPrice");
    $("#hotelNameSpan").text(hotelName);
    $("#selectedHotelName_p").text(hotelName);
    $("#roomTypeNameSpan").text(roomTypeName);
    $("#minPrice").html("&yen;" + arr[3]);
    $("#maxPrice").html("&yen;" + arr[4]);
    var elongBreakFast = jDiv.attr("elongBreakfast");
    if (elongBreakFast != "") {
        elongBreakFast = " (" + elongBreakFast + ")";
    }
    var haibiBreakfast = jDiv.attr("breakfast");
    if (haibiBreakfast != "") {
        haibiBreakfast = ' (' + haibiBreakfast + ')';
    }
    $("#haibiBreakfast").html(haibiBreakfast);
    $("#elongPriceSpan").html("&yen;" + elongPrice + elongBreakFast);
    $("#elongPrice_hidden").val(elongPrice);
    $("#hotelId_hidden").val(arr[0]);
    $("#preferenceBid_hidden").val(jDiv.attr("flag"));
    $("#elongRatePlaneId_hidden").val(jDiv.attr("elongRatePlaneId"));
    $("#roomTypeName_hidden").val(roomTypeName);
    $("#roomTypeId_hidden").val(arr[1]);
    $("#district_hidden").val(arr[6]);
    $("#business_hidden").val(arr[7]);
    $("#star_hidden").val(arr[8]);
    $("#chancePrice_hidden").val(arr[5]);
    $("#p_hidden").val(arr[2]);
    $("#breakfast_hidden").val(jDiv.attr("breakfast"));
    $("#bidInput").val(jDiv.attr("defaultBid"));
    minmoney = parseInt($("#minPrice").html().substr(1));
    maxmoney = parseInt($("#maxPrice").html().substr(1));
    $("#bidInput").keyup();
    $(".item").removeClass("current");
    jDiv.addClass("current");
}
function bookHotel() {
    var form = $("#hotelBidForm");
    /*if($("#activityType").val()==2){form.attr("action","/hotelGiveTicket.html");}else{*/
    form.attr("action", "/guonei/hotelBidBook.html"); //}
    var chancePrice = $("#chancePrice_hidden").val();
    var ep = $("#elongPrice_hidden").val();
    var p = $("#p_hidden").val();
    var s1 = $("#seed1_hidden").val();
    var s2 = $("#seed2_hidden").val();
    s1 = Number(s1);
    s2 = Number(s2);
    chancePrice = Number(chancePrice);
    p = Number(p);
    if (chancePrice > 0) {
        chancePrice = (chancePrice - s2) / s1;
    }
    if (p > 0) {
        p = (p - s2) / s1;
    }
    var bid = $("#bidInput").val();
    bid = Number(bid);
    if (bid <= 0 || chancePrice <= 0) {
        return;
    }
    var bidParams = "bid=" + bid + "&checkInDate=" + $("#checkInDate_hidden").val() + "&checkOutDate=" + $("#checkOutDate_hidden").val() + "&queryStr=" + encodeURIComponent($("#queryStr_hidden").val());
    bidParams = bidParams + "&hotelId=" + $("#hotelId_hidden").val() + "&roomTypeId=" + $("#roomTypeId_hidden").val() + "&roomTypeName=" + encodeURI(encodeURI($("#roomTypeName_hidden").val()));
    bidParams = bidParams + "&elongPrice=" + $("#elongPrice_hidden").val() + "&preferenceBid=" + $("#preferenceBid_hidden").val() + "&preferenceLink=" + $("#preferenceLink_hidden").val();
    bidParams = bidParams + "&cityCode=" + $("#cityCode_hidden").val();
    if (bid > p) {
        form[0].submit();
    } else {
        $("#popupbg,#popupbg .inner,#popupbg .iframe_popup").css("width", "100%");
        $("#popupbg,#popupbg .inner,#popupbg .iframe_popup").css("height", $(document).height());
        $("#popupbg,#popupbg .inner").css({
            "background": "#000",
            "position": "absolute",
            "filter": "alpha(opacity=50)",
            "opacity": "0.5",
            "z-index": "99998",
            "top": "0",
            "left": "0",
            "right": "0",
            "bottom": "0"
        });
        $("#popupbg .iframe_popup").css({
            "border": "none",
            "position": "absolute",
            "opacity": "0",
            "z-index": "99997"
        });
        $("#popupbg").show();
        $("#publicTip").show();
        var top = ($(window).height() - $("#publicTip").height()) / 2;
        var left = ($(window).width() - $("#publicTip").width()) / 2;
        var scrollTop = $(document).scrollTop();
        var scrollLeft = $(document).scrollLeft();
        $("#publicTip").css({
            "top": top + scrollTop,
            "left": left + scrollLeft
        });
        jQuery.ajax({
            url: "/doHotelBid.do",
            data: bidParams + "&bidSuccess=0",
            type: "post",
            dataType: "json",
            success: function(data) {}
        });
    }
}
function selectPlane(divEle) {
    var div = $(divEle);
    var preferenceBid = div.attr("preferenceBid");
    var ctripPrice = div.attr("ctripPrice");
    var leftPrice = div.attr("leftPrice");
    var rightPrice = div.attr("rightPrice");
    var successChancePrice = div.attr("successChancePrice");
    var haibiPrice = div.attr("haibiPrice");
    var haibiSalePrice = div.attr("haibiSalePrice");
    var arrTime1 = div.attr("arrTime1");
    var arrTime2 = div.attr("arrTime2");
    var arrTimeStr1 = div.attr("arrTimeStr1");
    var arrTimeStr2 = div.attr("arrTimeStr2");
    var dptTime1 = div.attr("dptTime1");
    var dptTime2 = div.attr("dptTime2");
    var dptTimeStr1 = div.attr("dptTimeStr1");
    var dptTimeStr2 = div.attr("dptTimeStr2");
    var midCode = div.attr("midCode");
    var flight1 = div.attr("flight1");
    var flight2 = div.attr("flight2");
    var airways1 = div.attr("airways1");
    var airways2 = div.attr("airways2");
    var airways1Name = div.attr("airways1Name");
    var airways2Name = div.attr("airways2Name");
    var dptDate1 = div.attr("dptDate1");
    var dptDate2 = div.attr("dptDate2");
    var model1 = div.attr("model1");
    var model2 = div.attr("model2");
    var modelType1 = div.attr("modelType1");
    var modelType2 = div.attr("modelType2");
    var modelType1Name = div.attr("modelType1Name");
    var modelType2Name = div.attr("modelType2Name");
    var dptDate2 = div.attr("dptDate2");
    var dptDate1 = div.attr("dptDate1");
    var flag = div.attr("flag");
    var midFlag = $("#midFlag").val();
    var cabin1 = div.attr("cabin1");
    var cabin2 = div.attr("cabin2");
    var discount1 = div.attr("discount1");
    var discount2 = div.attr("discount2");
    var fuel1 = div.attr("fuel1");
    var fuel2 = div.attr("fuel2");
    var tax1 = div.attr("tax1");
    var tax2 = div.attr("tax2");
    var meal1 = div.attr("meal1");
    var meal2 = div.attr("meal2");
    var share1 = div.attr("share1");
    var share2 = div.attr("share2");
    var stop1 = div.attr("stop1");
    var stop2 = div.attr("stop1");
    var fullPrice1 = div.attr("fullPrice1");
    var fullPrice2 = div.attr("fullPrice2");
    var price1 = div.attr("price1");
    var price2 = div.attr("price2");
    var sale1 = div.attr("sale1");
    var sale2 = div.attr("sale2");
    var premium1 = div.attr("premium1");
    var premium2 = div.attr("premium2");
    var notice1 = div.attr("notice1");
    var notice2 = div.attr("notice2");
    var extPolicyType1 = div.attr("extPolicyType1");
    var extPolicyType2 = div.attr("extPolicyType2");
    var extPolicyId1 = div.attr("extPolicyId1");
    var extPolicyId2 = div.attr("extPolicyId2");
    var extPolicyInfo1 = div.attr("extPolicyInfo1");
    var extPolicyInfo2 = div.attr("extPolicyInfo2");
    var validate1 = div.attr("validate1");
    var validate2 = div.attr("validate2");
    var dptCode = div.attr("dptCode");
    var arrCode = div.attr("arrCode");
    var dptName = div.attr("dptName");
    var arrName = div.attr("arrName");
    var midName = div.attr("midName");
    $("#airwaysCn1").text(airways1Name);
    $("#dptTime1").text(dptTimeStr1);
    $("#arrTime1").text(arrTimeStr1);
    $("#flight1").text(flight1);
    $("#modelType1").text(modelType1Name);
    $("#model2").text(model2);
    $("#minPrice").html("&yen;" + leftPrice);
    $("#maxPrice").html("&yen;" + rightPrice);
    $("#haibiPrice").html("&yen;" + haibiSalePrice);
    $("#ctripPrice").html("&yen;" + ctripPrice);
    if (midFlag == 1) {
        $("#dptCodeCn1").text(dptName);
        $("#arrCodeCn2").text(midName);
        $("#dptCodeCn21").text(midName);
        $("#arrCodeCn22").text(arrName);
    } else {
        $("#dptCodeCn1").text(dptName);
        $("#arrCodeCn2").text(arrName);
        $("#dptCodeCn21").text(arrName);
        $("#arrCodeCn22").text(dptName);
    }
    if (flag == 2 || midFlag == 1) {
        $("#airwaysCn2").text(airways2Name);
        $("#dptTime2").text(dptTimeStr2);
        $("#arrTime2").text(arrTimeStr2);
        $("#flight2").text(flight2);
        $("#modelType2").text(modelType2Name);
        $("#model2").text(model2);
    }
    $("#book\\.ctripPrice").val(ctripPrice);
    $("#chancePrice_hidden").val(successChancePrice);
    $("#r_hidden").val(rightPrice);
    $("#p_hidden").val(haibiPrice);
    $("#preferenceBid_hidden").val(preferenceBid);
    $("#book\\.dptTime1").val(dptTime1);
    $("#book\\.arrTime1").val(arrTime1);
    $("#book\\.flight1").val(flight1);
    $("#book\\.model1").val(model1);
    $("#book\\.modelType1").val(modelType1);
    $("#book\\.cabin1").val(cabin1);
    $("#book\\.discount1").val(discount1);
    $("#book\\.fuel1").val(fuel1);
    $("#book\\.tax1").val(tax1);
    $("#book\\.sale1").val(sale1);
    $("#book\\.fullPrice1").val(fullPrice1);
    $("#book\\.price1").val(price1);
    $("#book\\.sale1").val(sale1);
    $("#book\\.meal1").val(meal1);
    $("#book\\.stop1").val(stop1);
    $("#book\\.share1").val(share1);
    $("#book\\.premium1").val(premium1);
    $("#book\\.notice1").val(notice1);
    $("#book\\.validate1").val(validate1);
    $("#book\\.extPolicyType1").val(extPolicyType1);
    $("#book\\.extPolicyId1").val(extPolicyId1);
    $("#book\\.dptCode").val(dptCode);
    $("#book\\.arrCode").val(arrCode);
    if (flag == 2 || midFlag == 1) {
        if (midFlag == 1) {
            $("#book\\.midCode").val(midCode);
            if (dptTime1 > arrTime1 || dptTime2 < arrTime1) {
                var now = new Date(dptDate1.replace(/\-/g, "\/"));
                now.setDate(now.getDate() + 1);
                $("#dptDate2Mid").text(fmtDate(now));
            }
        }
        $("#book\\.dptTime2").val(dptTime2);
        $("#book\\.arrTime2").val(arrTime2);
        $("#book\\.flight2").val(flight2);
        $("#book\\.model2").val(model2);
        $("#book\\.modelType2").val(modelType2);
        $("#book\\.cabin2").val(cabin2);
        $("#book\\.discount2").val(discount2);
        $("#book\\.fuel2").val(fuel2);
        $("#book\\.tax2").val(tax2);
        $("#book\\.sale2").val(sale2);
        $("#book\\.fullPrice2").val(fullPrice2);
        $("#book\\.price2").val(price2);
        $("#book\\.sale2").val(sale2);
        $("#book\\.meal2").val(meal2);
        $("#book\\.stop2").val(stop2);
        $("#book\\.share2").val(share2);
        $("#book\\.premium2").val(premium2);
        $("#book\\.notice2").val(notice2);
        $("#book\\.validate2").val(validate2);
        $("#book\\.extPolicyType2").val(extPolicyType2);
        $("#book\\.extPolicyId2").val(extPolicyId2);
    }
    $("#bidInput").val(div.attr("defaultBid"));
    minmoney = parseInt($("#minPrice").html().substr(1));
    maxmoney = parseInt($("#maxPrice").html().substr(1));
    $("#bidInput").keyup();
    $(".item").removeClass("current");
    $(div).addClass("current");
}
function bookPlane() {
    var form = $("#planeBidForm");
    var maxprice = $("#maxPrice").attr("value");
    var biprice = $("#bidInput").val();
    if(parseInt(jQuery.trim(maxprice))==parseInt(jQuery.trim(biprice))){
    	 form.attr("action", "tc.do?m=order");
    	 form.submit();
    	 return;
    }
    var chancePrice = $("#chancePrice_hidden").val();
    var s1 = $("#seed1_hidden").val();
    var s2 = $("#seed2_hidden").val();
  
    s1 = Number(s1);
    s2 = Number(s2);
    chancePrice = Number(chancePrice);
    if (chancePrice > 0) {
        chancePrice = (chancePrice - s2) / s1;
    }
    var p = $("#p_hidden").val();
    p = Number(p);
    if (p > 0) {
        p = (p - s2) / s1;
    }
    if (p <= 0 || chancePrice <= 0) {
        return;
    }
    var rp = $("#r_hidden").val();
    rp = Number(rp);
    var bid = $("#bidInput").val();
    if ((bid > chancePrice && chancePrice < rp) || (chancePrice >= rp && bid >= p)) {
        form[0].submit();
    } else {
        var bidParams = "bidInput=" + bid + "&book.flag=" + $("#searchFlagC2b").val();
        bidParams = bidParams + "&book.dptCode=" + $("#dptCodeC2b").val();
        bidParams = bidParams + "&book.arrCode=" + $("#arrCodeC2b").val();
        bidParams = bidParams + "&book.dptTime=" + $("#dptTimeC2b").val();
        bidParams = bidParams + "&book.dptDate1=" + $("#dptDate1C2b").val();
        if ($("#searchFlagC2b").val() == 2) {
            bidParams = bidParams + "&book.dptDate2=" + $("#dptDate2C2b").val();
        }
        bidParams = bidParams + "&book.ctripPrice=" + $("#book\\.ctripPrice").val() + "&book.preferenceBid=" + $("#preferenceBid_hidden").val() + "&book.preferenceLink=" + $("#preferenceLink_hidden").val();
        jQuery.ajax({
            url: "/guonei/planeBidBookSecond.do",
            data: bidParams + "&book.bidSuccess=0",
            type: "post",
            dataType: "json",
            success: function(data) {}
        });
        $("#popupbg,#popupbg .inner,#popupbg .iframe_popup").css("width", "100%");
        $("#popupbg,#popupbg .inner,#popupbg .iframe_popup").css("height", $(document).height());
        $("#popupbg,#popupbg .inner").css({
            "background": "#000",
            "position": "absolute",
            "filter": "alpha(opacity=50)",
            "opacity": "0.5",
            "z-index": "99998",
            "top": "0",
            "left": "0",
            "right": "0",
            "bottom": "0"
        });
        $("#popupbg .iframe_popup").css({
            "border": "none",
            "position": "absolute",
            "opacity": "0",
            "z-index": "99997"
        });
        $("#popupbg").show();
        $("#publicTip").show();
        var top = ($(window).height() - $("#publicTip").height()) / 2;
        var left = ($(window).width() - $("#publicTip").width()) / 2;
        var scrollTop = $(document).scrollTop();
        var scrollLeft = $(document).scrollLeft();
        $("#publicTip").css({
            "top": top + scrollTop,
            "left": left + scrollLeft
        });
    }
}
function fmtDate(d) {
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    return year + '-' + month + '-' + day;
}
jQuery(document).ready(function() {
    var dptTime1 = $("#book\\.dptTime1").val();
    var arrTime1 = $("#book\\.arrTime1").val();
    var dptTime2 = $("#book\\.dptTime2").val();
    var dptDate1 = $("#book\\.dptDate1").val();
    if (dptTime1 > arrTime1 || dptTime2 < arrTime1) {
        var now = new Date(dptDate1.replace(/\-/g, "\/"));
        now.setDate(now.getDate() + 1);
        $("#dptDate2Mid").text(fmtDate(now));
    }
});